let app =getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab_id:2,
    price:[],
    yue:''
  },
  closeTip(){
    this.setData({
      setSuccess: false,
      setErr: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that =this;
    wx.request({
      url: app.data.url +'/diposit_show',
      header:app.getHeader(),
      success:function(res){
          that.setData({
            yue: res.data.card_money
          })
      }
    })
    wx.request({
      url: app.data.url + '/recharge',
      header: app.getHeader(),
      success: function (res) {
        that.setData({
          price: res.data.rechargeArr
        })
      }
    })
  },
  active:function(e){
    var that =this;
    var id =e.currentTarget.dataset.tab;
    that.setData({
      tab_id:id
    })
  },
  not(){
    return false
  },
  submit:function(){
    var that = this;
    var price = that.data.price[that.data.tab_id - 1].denomination
    var jian = that.data.price[that.data.tab_id - 1].preferential
    wx.request({
      url: app.data.url + '/diposit_order',
      data: {
        price: price,
        preferential: jian
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: app.getHeader(),
      success: function (res) {
        var order = res.data;
        wx.requestPayment({
          'timeStamp': order.timeStamp,
          'nonceStr': order.nonceStr,
          'package': order.package,
          'signType': order.signType,
          'paySign': order.paySign,
          success: function (res) {
            that.setData({
              czSuccess: true,
              setprice: price,
              setpreferential: jian,
              setstatus: 1
            })
            
          },
          fail: function (res) {
            that.setData({
              czSuccess: true,
              setprice: price,
              setpreferential: jian,
              setstatus: 0
            })
          }
        })
      },
      fail: function () {
        // fail
        app.toast('网络异常！err:wxpay');
      }
    })
  },
  closeTips(){
    this.setData({
      czSuccess: false
    })
    if (!app.data.passSet) {
      this.setData({
        setPass: true
      })
    }
    let datas = this.data
    if (datas.setstatus == 1){
      let all = parseInt(datas.yue) + parseInt(datas.setprice) + parseInt(datas.setpreferential)
      this.setData({
        yue: all
      })
    }
  },
  pwd: function (e) {
    var that = this;
    var date = e.detail.value
    that.setData({
      pwd: date
    })
  },
  r_pwd: function (e) {
    var that = this;
    var date = e.detail.value
    that.setData({
      r_pwd: date
    })
  },
  changePass(){
    const that = this;
    var pwd = that.data.pwd;
    var r_pwd = that.data.r_pwd;
    if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/.test(pwd)) {
      wx.showModal({
        title: '提示',
        content: '请输入正确格式的密码！',
        showCancel: false
      })
      return false
    }
    if (pwd != r_pwd) {
      wx.showModal({
        title: '提示',
        content: '两次输入不一致！',
        showCancel: false
      })
      return false
    }
    wx.request({
      url: app.data.url + '/setPassword',
      method: "POST",
      header: app.getHeader(),
      data: {
        password: that.data.pwd,
        repassword: that.data.r_pwd
      },
      success: function (res) {
        if (res.data.tips == 200) {
          that.setData({
            setPass: false,
            setSuccess: true
          })
        } else {
          that.setData({
            setPass: false,
            setErr: true
          })
        }
        setTimeout(function(){
          that.setData({
            setSuccess: false,
            setErr: false
          })
        },10000)
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  goLog(){
    wx.navigateTo({
      url: '../rechargeLog/rechargeLog',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})